package net.lzzy.practices.web.admin;

import io.swagger.annotations.Api;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author m
 */

@CrossOrigin //允许跨域
@RestController
@RequestMapping(ApiConstant.ROUTE_USER_ROOT)
@Api(tags = "用户相关业务")
public class UserApiController {
    private  final UserService service;
@Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }
    @GetMapping(ApiConstant.ROUTE_USER_ALL)
    public ApiResult getUsers(@RequestParam String token){
    List<VmUser> users = new ArrayList<>();
    //通过遍历将每一个数据添加到users集合里面
    service.get().forEach(u -> users.add(VmUser.create(u,true)));
    return  new ApiResult(true,users.size()+"",users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_PAGE)
    public  ApiResult getPagedUsers(@RequestParam int page,@RequestParam int size,@RequestParam String token){
        List<VmUser> users = new ArrayList<>();
        //通过遍历将每一个数据添加到users集合里面
        service.get().forEach(u -> users.add(VmUser.create(u,true)));
        return  new ApiResult(true,service.count()+"",users);
    }
//    搜索
    @GetMapping(ApiConstant.ROUTE_USER__SEARCH)
    public  ApiResult search(@RequestParam String token,@PathVariable String kw){
        List<VmUser> users = new ArrayList<>();
        //通过遍历将每一个数据添加到users集合里面
        service.get().forEach(u -> users.add(VmUser.create(u,true)));
        return  new ApiResult(true,users.size()+"",users);

    }



//查询在申请老师的用户
    @GetMapping(ApiConstant.ROUTE_USER_APPLYING)
    public  ApiResult getApplyingTeacher(@RequestParam String token){
        List<VmUser> users = new ArrayList<>();
        //通过遍历将每一个数据添加到users集合里面
        service.get().forEach(u -> users.add(VmUser.create(u,true)));
        return  new ApiResult(true,users.size()+"",users);
    }
    //是否通过用户老师申请
    @PostMapping(ApiConstant.ROUTE_USER_APPROVE)
    public ApiResult approve(@RequestParam String token, @RequestParam UUID id ){
        User user = service.approveTeacjer(id);
        if (user==null){
            return new  ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        return  new ApiResult(true, Messages.SUCCESC.toString(), user);

    }
    @PostMapping(ApiConstant.ROUTE_USER_DECLINE)
    public ApiResult decline(@RequestParam String token, @RequestParam UUID id ){
        User user= service.declineTeacher(id);
        if (user==null){
            return new  ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        return  new ApiResult(true, Messages.SUCCESC.toString(), user);
    }

    @PostMapping(ApiConstant.ROUTE_USER_BAN)
    public  ApiResult ban(@RequestParam String token,@RequestParam UUID uuid){
    User user = service.banUser(uuid);
    if(user==null){
        return  new ApiResult(false,Messages.WRONG_ID.toString(),null);
    }
    return  new ApiResult(true,Messages.SUCCESC.toString(), user);
    }


}
